﻿namespace System.IO
{
    public abstract class InMemoryFileSystemEntryInfo : IFileSystemInfo
    {
        private readonly InMemoryFileSystemStore _store;
        private readonly InMemoryFileSystemEntryProperties _entry;
        private readonly string _name;

        protected InMemoryFileSystemStore Store { get { return _store; } }

        protected InMemoryFileSystemEntryProperties ExistingEntry
        {
            get
            {
                if (_entry == null || !_entry.Exists) throw new IOException("File or directory does not exist.");
                return _entry;
            }
        }

        public InMemoryFileSystemEntryInfo(InMemoryFileSystemStore store, InMemoryFileSystemEntryProperties entry)
        {
            if (store == null) throw new ArgumentNullException("store");
            if (entry == null) throw new ArgumentNullException("entry");

            _store = store;
            _entry = entry;
        }

        public InMemoryFileSystemEntryInfo(InMemoryFileSystemStore store, string name)
        {
            if (store == null) throw new ArgumentNullException("store");
            if (name == null) throw new ArgumentNullException("name");

            _store = store;
            _name = name;
        }

        public void Delete()
        {
            _store.Delete(_entry.FullPath);
        }

        public void Refresh()
        {
        }

        public string FullName
        {
            get { return _entry == null ? _name : _entry.FullPath; }
        }

        public string Extension
        {
            get { return LongPathTools.Instance.GetExtension(FullName); }
        }

        public abstract string Name { get; }

        public bool Exists
        {
            get { return _entry != null && _entry.Exists; }
        }

        public DateTime CreationTime
        {
            get { return ExistingEntry.CreationTimeUtc.ToLocalTime(); }
            set { ExistingEntry.CreationTimeUtc = value.ToUniversalTime(); }
        }

        public DateTime CreationTimeUtc
        {
            get { return ExistingEntry.CreationTimeUtc; }
            set { ExistingEntry.CreationTimeUtc = value; }
        }

        public DateTime LastAccessTime
        {
            get { return ExistingEntry.LastAccessTimeUtc.ToLocalTime(); }
            set { ExistingEntry.LastAccessTimeUtc = value.ToUniversalTime(); }
        }

        public DateTime LastAccessTimeUtc
        {
            get { return ExistingEntry.LastAccessTimeUtc.ToLocalTime(); }
            set { ExistingEntry.LastAccessTimeUtc = value.ToUniversalTime(); }
        }

        public DateTime LastWriteTime
        {
            get { return ExistingEntry.LastWriteTimeUtc.ToLocalTime(); }
            set { ExistingEntry.LastWriteTimeUtc = value.ToUniversalTime(); }
        }

        public DateTime LastWriteTimeUtc
        {
            get { return ExistingEntry.LastWriteTimeUtc.ToLocalTime(); }
            set { ExistingEntry.LastWriteTimeUtc = value.ToUniversalTime(); }
        }

        public FileAttributes Attributes
        {
            get { return ExistingEntry.Attributes; }
            set { ExistingEntry.Attributes = value; }
        }
    }
}